package com.bysc.commentservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commentservice.entity.CommentType;
import com.bysc.commentservice.entity.vo.CommentTypeVo;
import com.bysc.commentservice.service.CommentTypeService;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-05-28
 */
@RestController
@Api(tags = "帖子类型管理")
@RequestMapping("/commentservice/comment-type")
public class CommentTypeController {

    @Autowired
    private CommentTypeService commentTypeService;

    @ApiOperation(value = "新增或修改评论类别")
    @PostMapping("addOrUpdateParentCommentType")
    public Results addOrUpdateParentCommentType(@ApiParam(name = "CommentType", value = "评论类别", required = true)
                                                @RequestBody CommentType commentType) {
        try {
            if (StringUtils.isEmpty(commentType.getParentId())) {
                commentType.setParentId("0");
            }
            commentTypeService.saveOrUpdate(commentType);
            return Results.ok().message(MessageConstant.EDIT_COMMENTTYPE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.EDIT_COMMENTTYPE_FAIL);
        }
    }


    @ApiOperation(value = "查询所有一级帖子类别")
    @GetMapping("getParentCommentType")
    public Results getParentCommentType() {
        try {
            LambdaQueryWrapper<CommentType> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CommentType::getParentId, 0);
            wrapper.orderByDesc(CommentType::getSort);
            List<CommentType> parentList = commentTypeService.list(wrapper);
            return Results.ok().data("parentList", parentList).message(MessageConstant.QUERY_COMMENTTYPE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.QUERY_COMMENTTYPE_FAIL);
        }
    }

    @ApiOperation(value = "查询所有帖子类别")
    @GetMapping("getAllCommentType")
    public Results getAllCommentType() {
        try {
            LambdaQueryWrapper<CommentType> pwrapper = new LambdaQueryWrapper<>();
            pwrapper.eq(CommentType::getParentId, 0);
            pwrapper.orderByDesc(CommentType::getSort);

            List<CommentType> parentList = commentTypeService.list(pwrapper);//父类型
            LambdaQueryWrapper<CommentType> wrapper = new LambdaQueryWrapper<>();
            wrapper.ne(CommentType::getParentId, 0);
            wrapper.orderByDesc(CommentType::getSort);

            List<CommentType> list = commentTypeService.list(wrapper);//子类型
            CommentTypeVo[] commentTypeVos = new CommentTypeVo[parentList.size()];
            int index = 0;
            for (CommentType commentType : parentList) {
                ArrayList<CommentType> commentTypes = new ArrayList<>();
                for (CommentType type : list) {
                    if (type != null && commentType != null && !StringUtils.isEmpty(type.getParentId()) && !StringUtils.isEmpty(type.getParentId()) && type.getParentId().equals(commentType.getId())) {
                        commentTypes.add(type);
                    }
                }
                commentTypeVos[index++] = new CommentTypeVo(commentType, commentTypes);
            }
            return Results.ok().data("List", commentTypeVos).message(MessageConstant.QUERY_COMMENTTYPE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.QUERY_COMMENTTYPE_FAIL);
        }
    }

    @ApiOperation(value = "根据parentId查询帖子类别")
    @GetMapping("getCommentTypeByParentId/{parentId}")
    public Results getCommentTypeByParentId(@ApiParam(name = "parentId", value = "父类别ID", required = true)
                                            @PathVariable String parentId) {
        try {
            LambdaQueryWrapper<CommentType> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CommentType::getParentId, parentId);
            List<CommentType> sonList = commentTypeService.list(wrapper);
            return Results.ok().data("sonList", sonList).message(MessageConstant.QUERY_COMMENTTYPE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.QUERY_COMMENTTYPE_FAIL);
        }
    }

    @ApiOperation(value = "根据类别Id删除帖子类别")
    @DeleteMapping("deleteCommentTypeById/{id}")
    public Results deleteCommentTypeById(@ApiParam(name = "id", value = "类别ID", required = true)
                                         @PathVariable String id) {
        try {
            CommentType commentType = commentTypeService.getById(id);
            String parentId = commentType.getParentId();
            if (parentId.equals("0")) {
                List<CommentType> list = commentTypeService.list(new QueryWrapper<CommentType>().eq("parent_id", id));
                if (!ObjectUtils.isEmpty(list)) {
                    commentTypeService.removeSonCommentTypeById(id);
                }
            }
            commentTypeService.removeById(id);
            return Results.ok().message(MessageConstant.DELETE_COMMENTTYPE_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,MessageConstant.DELETE_COMMENTTYPE_FAIL);
        }

    }

}
